<template>
    <base-breadcrumb>
        <searchData :searchData="searchArr" @search="search"></searchData>
        <a-card>
            <div>
                <a-button
                    class="mb12"
                    v-btnPermission="'bm_020_add_01'"
                    type="primary"
                    icon="plus"
                    @click="handleUpdate('add', {})"
                    >新增</a-button
                >
                <a-alert type="info" show-icon>
                    <template slot="message">
                        <span>
                            个人初始预留风险金(元):
                            <a>{{ statistics.allInitialRiskFee }}</a>
                        </span>
                        <a-divider type="vertical" />
                        <span>
                            已返还(元):
                            <a>{{ statistics.allHasBackFee }}</a>
                        </span>
                        <a-divider type="vertical" />
                        <span>
                            剩余预留风险金(元):
                            <a>{{ statistics.allLastRiskFee }}</a>
                        </span>
                    </template>
                </a-alert>
            </div>
            <list-table
                ref="table"
                size="default"
                rowKey="key"
                :columns="columns"
                :request="loadData"
                :alert="true"
                show-size-changer
                show-quick-jumper
                showPagination="auto"
            >
                <span slot="description" slot-scope="text">
                    <ellipsis :length="8" tooltip>{{ text }}</ellipsis>
                </span>
                <span slot="operation" slot-scope="text, record">
                    <MoreButton>
                    <a v-btnPermission="'bm_020_loo_04'" @click="handleUpdate('look', record)">查看</a>
                    <span
                        v-btnPermission="'bm_020_edi_02'"
                        v-if="
                            record.auditStatus != 'SUBMITTED' &&
                            record.auditStatus != 'PAST' &&
                            record.auditStatus != 'ABOLISH'
                        "
                    >
                        <a @click="handleUpdate('edit', record)">编辑</a>
                    </span>
                    <!-- 撤销 -->
                    <span v-if="record.auditStatus == 'SUBMITTED' && userId === record.creatorId&&hideBuquBtn()">
                        <a @click="handleUndo({ id: record.id, type: 3 })">撤销</a>
                    </span>
                    <!-- 作废 -->
                    <span v-if="record.auditStatus == 'PAST'" v-btnPermission="'bm_020_tov_05'">
                        <a @click="handleInvalid({ id: record.id, type: 2 })">作废</a>
                    </span>
                    <!-- 删除 -->
                    <span
                        v-if="record.auditStatus == 'DRAFT' || record.auditStatus == 'REJECTED'"
                        v-btnPermission="'bm_020_del_03'"
                    >
                        <a @click="handleRemoveParams({ id: record.id, type: 1 })">删除</a>
                    </span>

                    <span
                        v-btnPermission="'bm_020_loo_04'"
                        v-if="
                            record.auditStatus === 'PAST' ||
                            record.auditStatus === 'SUBMITTED' ||
                            record.auditStatus === 'REJECTED' ||
                            record.auditStatus === 'ABOLISH'
                        "
                    >
                        <approval-button :status="record.auditStatus" :id="record.id"></approval-button>
                    </span>
                        </MoreButton>
                </span>
                <span slot="auditStatus" slot-scope="text, record">
                    <list-status
                        :status="record.auditStatus"
                        :isApproval="true"
                        conversion="en"
                        :text="text"
                    ></list-status>
                </span>
            </list-table>
        </a-card>
    </base-breadcrumb>
</template>

<script>
import BasePage from '@/views/basePage' // 基础配置
import { getPersonalRiskReturn, delPersonalRiskReturn } from '@/api/businessAffairs/riskRefund'
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
const columns = [
    {
        title: '所属分公司',
        dataIndex: 'branchComName'
    },
    {
        title: '所属项目',
        dataIndex: 'projectName'
    },
    {
        title: '已返还（元）',
        dataIndex: 'hasReturnAmount'
    },
    {
        title: '本次返还（元）',
        dataIndex: 'returnAmount'
    },
    {
        title: '剩余预留风险金（元）',
        dataIndex: 'newReservedRiskFund'
    },

    {
        title: '申请人',
        dataIndex: 'returnApplier'
    },

    {
        title: '填报时间',
        dataIndex: 'createTime'
    },
    {
        title: '状态',
        dataIndex: 'auditStatus',
        scopedSlots: { customRender: 'auditStatus' }
    },
    {
        title: '操作',
        dataIndex: 'operation',
        width: '230px',
        scopedSlots: { customRender: 'operation' }
    }
]
const searchArr = [
{
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
    // {
    //     name: '所属分公司',
    //     valueKey: 'branchComId',
    //     type: SelectSubCompany
    // },
    // {
    //     name: '所属项目',
    //     keyValue: 'branchComId',
    //     valueKey: 'projectId',
    //     type: SelectProject
    // },
    {
        name: '填报人',
        value: 'creatorName',
        type: 'input'
    },
    {
        name: '填报时间',
        value: 'startEndDate',
        type: 'dateRange'
    },
    {
        name: '状态',
        value: 'auditStatus',
        type: 'select',
        data: typeListApproval
    }
]
import { typeListApproval } from '@/utils/util'
import { STable } from '@/components'
export default {
    name: 'a' + Date.now(),
    extends: new BasePage(),
    components: {
        STable
    },
    data() {
        this.columns = columns
        this.searchArr = searchArr
        return {
            queryParam: {},
            userId: JSON.parse(sessionStorage.getItem('ycloud-user_info')).id,
            removeApi: {
                remove: delPersonalRiskReturn,
                invalid: delPersonalRiskReturn,
                undo: delPersonalRiskReturn
            }, // api
            statistics: { allHasBackFee: 0, allInitialRiskFee: 0, allLastRiskFee: 0 },
            loadData: async (parameter) => {
                const requestParameters = Object.assign({}, parameter, this.queryParam)
                const [err, res] = await getPersonalRiskReturn(requestParameters)
                return new Promise((resolve, reject) => {
                    if (err) {
                        reject(err)
                    } else {
                        //数组会默认有一条数据来显示统计
                        this.statistics.allHasBackFee = res.data[0].allHasBackFee
                        this.statistics.allInitialRiskFee = res.data[0].allInitialRiskFee
                        this.statistics.allLastRiskFee = res.data[0].allLastRiskFee
                        if (res.data[0].id) {
                            resolve(res)
                        } else {
                            res.data = []
                            resolve(res)
                        }
                    }
                })
            }
        }
    },
    methods: {
        //搜索
        search(val) {
            this.queryParam = {
                ...val,
                createTimeBegin:
                    (val.startEndDate &&
                        val.startEndDate.length &&
                        val.startEndDate[0].format('YYYY-MM-DD HH:mm:ss')) ||
                    void 0,
                createTimeEnd:
                    (val.startEndDate &&
                        val.startEndDate.length &&
                        val.startEndDate[1].format('YYYY-MM-DD HH:mm:ss')) ||
                    void 0
            }
            delete this.queryParam.startEndDate
            this.$refs.table.refresh(true)
        },
        async delPersonalRiskReturn(data) {
            const [err, res] = await delPersonalRiskReturn({ id: data.id })
            if (!err) {
                this.$message.success('成功')
                this.$refs.table.refresh(true)
            }
        },
        //新增
        async handleUpdate(type, data = {}) {
            if (type == 'del') {
                let that = this
                this.$confirm({
                    title: '温馨提示',
                    content: '确定是否删除数据？',
                    okText: '确认',
                    okType: 'danger',
                    cancelText: '取消',
                    onOk: () => {
                        return that.delPersonalRiskReturn(data)
                    }
                })
            } else {
                this.$router.push({
                    name: 'updatePersonalRiskRefund',
                    query: {
                        id: data.id || '',
                        pageType: type
                    }
                })
            }
        }
    }
}
</script>


